package com.zzjc.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzjc.web.entity.SwimmingPool;
import com.zzjc.web.mapper.SwimmingPoolDao;
import com.zzjc.web.service.SwimmingPoolService;
import com.zzjc.web.util.ApiResponse;
import com.zzjc.web.util.DateUtils;
import com.zzjc.web.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SwimmingPoolServiceImpl implements SwimmingPoolService {

    @Autowired
    private SwimmingPoolDao swimmingPoolDao;

    @Override
    public ApiResponse addPool(SwimmingPool pool) {
        try {
            SwimmingPool existingPool = swimmingPoolDao.getPoolByName(pool.getPoolName());
            if (existingPool != null) {
                return ResponseUtil.error("游泳池已存在,无需重复添加");
            }
            pool.setCreateTime(DateUtils.getNowDate());
            int result = swimmingPoolDao.insertPool(pool);
            return ResponseUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("添加游泳池失败请重试");
        }
    }

    @Override
    public ApiResponse<?> updatePool(SwimmingPool pool) {
        if (pool.getPoolId() != null) {
            try {
                List<Integer> poolIdList = new ArrayList();
                poolIdList.add(pool.getPoolId());
                List<SwimmingPool> iPool = swimmingPoolDao.getPoolById(poolIdList);
                if (iPool.size() < 1) {
                    return ResponseUtil.error("游泳池不存在");
                } else {
                    SwimmingPool existingPool = swimmingPoolDao.getPoolByName(pool.getPoolName());
                    if (existingPool != null) {
                        return ResponseUtil.error("该游泳名称已存在,无需重复添加");
                    }
                    pool.setCreateTime(DateUtils.getNowDate());
                    int result = swimmingPoolDao.updatePool(pool);
                    return ResponseUtil.success();
                }

            } catch (Exception e) {
                e.printStackTrace();
                return ResponseUtil.error("修改游泳池失败");
            }
        } else {
            return ResponseUtil.error("游泳池id不能为空");
        }
    }

    @Override
    public ApiResponse deletePool(List<Integer> poolList) {
        if (poolList.size() > 0) {
            try {
                List<SwimmingPool> poolById = swimmingPoolDao.getPoolById(poolList);
                if (poolById.size() < 1) {
                    return ResponseUtil.error("游泳池不存在");
                } else {
                    swimmingPoolDao.deletePool(poolList);
                    return ResponseUtil.success();
                }

            } catch (Exception e) {
                e.printStackTrace();
                return ResponseUtil.error("修改游泳池失败");
            }
        } else {
            return ResponseUtil.error("id不能为空");


        }
    }


    @Override
    public ApiResponse getAllPools(SwimmingPool pool) {
        PageInfo<SwimmingPool> pageInfo = null;
        try {
            int pageNum = 1;
            if (pool.getPageNum() != null) {
                pageNum = pool.getPageNum();
            }
            int pageSize = 1000;
            if (pool.getPageSize() != null) {
                pageSize = pool.getPageSize();
            }
            PageHelper.startPage(pageNum, pageSize);
            List<SwimmingPool> poolList = swimmingPoolDao.findAll(pool);
            pageInfo = new PageInfo<>(poolList);
            return ResponseUtil.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("查询失败，请重试");
        }

    }

}